﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

using ScytheEngine;

using VTESCore;

namespace VTESGameLogicWindows
{
	public class CardObject : ScytheEngine.Object,
		ILoadable,
		IRenderable,
		IUpdateable
	{
		VTESCard m_Card;
		Texture2D m_CardTexture;
		float m_SpriteRotation;

		public CardObject(VTESCard card)
		{
			m_Card = card;
		}

		#region IRenderable Members

		public void Render( Microsoft.Xna.Framework.Graphics.GraphicsDevice gfxDev )
		{
			SpriteBatch spriteBatch = new SpriteBatch( gfxDev );
			Viewport viewport = gfxDev.Viewport;
			Vector2 card_pos = new Vector2( viewport.Width / 2 - m_CardTexture.Width / 2, viewport.Height / 2 - m_CardTexture.Height / 2 );
			Rectangle card_rect = new Rectangle( (int)card_pos.X, (int)card_pos.Y, m_CardTexture.Width, m_CardTexture.Height );

			spriteBatch.Begin( SpriteBlendMode.None );

			spriteBatch.Draw( m_CardTexture, card_rect, null, Color.White, m_SpriteRotation, 
				new Vector2(m_CardTexture.Width/2, m_CardTexture.Height/2), SpriteEffects.None, 0);

			spriteBatch.End();
		}

		#endregion

		#region ILoadable Members

		public void LoadContent( GraphicsDevice gfxDev, Microsoft.Xna.Framework.Content.ContentManager loader )
		{
			ContentManager card_images_content = new ContentManager( loader.ServiceProvider, "Content\\cardimages" + Path.GetDirectoryName( m_Card.CardArtPath ) );
			m_CardTexture = card_images_content.Load<Texture2D>( m_Card.TextureID );
		}

		#endregion

		#region IUpdateable Members

		public bool Enabled
		{
			get { return true; }
		}

		public event EventHandler  EnabledChanged;

		public int UpdateOrder
		{
			get { return 0; }
		}

		public event EventHandler  UpdateOrderChanged;

		public void Update(GameTime gameTime)
		{
			m_SpriteRotation = (float)gameTime.TotalGameTime.TotalSeconds * 2;
		}

		#endregion
	}
}
